package com.keke.limiter.redis.config.factory;


import com.keke.limiter.peimitlimiter.Constants;
import com.keke.limiter.redis.config.RedisConfig;
import com.keke.limiter.redis.key.common.PermitBucketKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Service
public class RedisPoolFactory {

    @Autowired
    RedisConfig redisConfig;

    @Bean
    public JedisPool jedisPoolFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
        poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
        poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000);
        JedisPool jp = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(),
                redisConfig.getTimeout() * 1000, redisConfig.getPassword());
        //每次启动删除redis中令牌桶的缓存
        jp.getResource().del(PermitBucketKey.permitBucket.getPrefix() + Constants.LIMI_NAME);
        return jp;
    }
}
